package sk7;

import javax.swing.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 学生管理主界面
 */
public class StudentUI1 {
    Scanner scanner = new Scanner(System.in);
    SuperArray ary = new SuperArray();
    public String namePatten ="[\\u4e00-\\u9fa5]{0,}";
    public String genderPatten ="[男女]";
    /**
     * 菜单
     * @return
     */
    public int denglu(){
        while (true) {
            String input = JOptionPane.showInputDialog("1-添加学生\n 2-删除学生\n 3-修改学生\n 4-查找学生\n 5-退出");
            if (input == null) {
                JOptionPane.showMessageDialog(null, "程序已退出");
                System.exit(0);}

                int index = Integer.parseInt(input);
                switch (index) {
                    case 1:
                        add();
                        break;
                    case 2:
                        remove();
                        break;
                    case 3:
                        change();
                        break;
                    case 4:
                        find();
                        break;
                    case 5:
                        JOptionPane.showMessageDialog(null, "感谢使用");
                        System.exit(1);
                    default:
                        JOptionPane.showMessageDialog(null, "输入错误");
                        continue;
                }
            }
        }



    /**
     * 添加学生
     */
    public void add(){
        String name = JOptionPane.showInputDialog("请输入学生姓名：");
        if (!name.matches(namePatten)){
            JOptionPane.showMessageDialog(null,"名字请输入中文");
            return;
        }
        if (name == null) {
            JOptionPane.showMessageDialog(null, "程序已退出");
            System.exit(0);}
        String ag = JOptionPane.showInputDialog("请输入学生年龄：");
        if (ag == null) {
            JOptionPane.showMessageDialog(null, "程序已退出");
            System.exit(0);}
        String gender = JOptionPane.showInputDialog("请输入学生性别：");
        if (!gender.matches(genderPatten)) {
            JOptionPane.showMessageDialog(null, "请输入男或女");
            return;
        }

        if (gender == null) {
            JOptionPane.showMessageDialog(null, "程序已退出");
            System.exit(0);}


        int age=Integer.parseInt(ag);
        Student student = new Student(name, age, gender);
        ary.add(student);
        JOptionPane.showMessageDialog(null,"添加成功");

    }

    /**
     * 删除学生
     */
    public void remove(){
        String name = JOptionPane.showInputDialog("请输入学生姓名");
        if (name == null) {
            JOptionPane.showMessageDialog(null, "程序已退出");
            System.exit(0);}

        for (int i = 0; i < ary.size(); i++){
            Student student =  (Student) ary.get(i);
            if (student.getName().equals(name)){
                ary.remove(i);
                JOptionPane.showMessageDialog(null,"删除成功\n");
                return;
            }

        }
        JOptionPane.showMessageDialog(null,"未查找到相关学生");


    }

    /**
     * 修改学生
     */
    public void change(){
        String name = JOptionPane.showInputDialog("请输入学生姓名");
        if (name == null) {
            JOptionPane.showMessageDialog(null, "程序已退出");
            System.exit(0);}
        for (int i = 0; i < ary.size(); i++){
            Student student =  (Student) ary.get(i);
            if (student.getName().equals(name)){
                ary.remove(i);
                add();
                JOptionPane.showMessageDialog(null,"修改成功");
                return;
            }

        }
        JOptionPane.showMessageDialog(null,"未查找到相关学生");
    }

    /**
     * 查找学生
     */
    public void find() {
        String student1 = "";
        String student2="";
        String name = JOptionPane.showInputDialog("请输入学生姓名（查询全部学生输入666）");
        Pattern pattern = Pattern.compile(name);
        if (name == null) {
            JOptionPane.showMessageDialog(null, "程序已退出");
            System.exit(0);
        }
        if (name.equals("666")) {
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.get(i);
                student1 +=
                        "姓名：" + student.getName() +
                                "，年龄：" + student.getAge() +
                                "，性别：" + student.getGender() +
                                "\n";
            }
            JOptionPane.showMessageDialog(null, student1);


        } else {
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.get(i);
                Matcher m = pattern.matcher(student.getName());
                if (m.find()) {
                    JOptionPane.showMessageDialog(null, ary.get(i));
                    student2 +=
                            "姓名：" + student.getName() +
                                    "，年龄：" + student.getAge() +
                                    "，性别：" + student.getGender() +
                                    "\n";
                }else {
                    JOptionPane.showMessageDialog(null, "未查找到相关学生");
                }
                }
            JOptionPane.showMessageDialog(null, student2);

            }

        }


    public static void main(String[] args) {
        StudentUI1 studentUI1 = new StudentUI1();
        studentUI1.denglu();
    }

}

